package com.zues.samples.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zues.samples.dao.UserDAO;
import com.zues.samples.model.PagerModel;
import com.zues.samples.model.Subject;
import com.zues.samples.service.SubjectService;
import com.zues.samples.service.modelVO.SubjectVO;

@Controller
public class SubjectController {

	@Autowired
	private SubjectService subjectService;
	
	@Autowired
	private UserDAO userDAO;

	/**
	 * 列出所有subject
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "listsubject", method = RequestMethod.GET)
	private String subjectList(Model model) throws Exception {
		model.addAttribute("subject", new Subject());
		PagerModel<SubjectVO> list = subjectService.findAllByPage();
		model.addAttribute("subjectList", list);
		return "teacher/subjectlist";
	}

	/**
	 * 添加一个科目
	 * 
	 * @param redirectAttributes
	 * @param subject
	 * @return
	 */
	@RequestMapping(value = "addSubject", method = RequestMethod.POST)
	private String addSubject(RedirectAttributes redirectAttributes, Subject subject) {
		try {
			subjectService.createSubject(subject);
			redirectAttributes.addAttribute("message", "添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "redirect:/listsubject";
	}

	/**
	 * 通过ID查找到Subject
	 * 
	 * @param model
	 * @param id
	 * @throws Exception
	 */
	@RequestMapping(value = "/subedit/{subid}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> toedit(Model model, @PathVariable int subid) throws Exception {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if (subid > 0) {
			Subject subject = subjectService.getByIdSubject(subid);
			modelMap.put("subject", subject);
			modelMap.put("success", true);
		} else {
			modelMap.put("success", false);
		}
		return modelMap;

	}

	/**
	 * 修改科目信息
	 * 
	 * @param redirectAttributes
	 * @param subject
	 * @param subid
	 * @return
	 */
	@RequestMapping(value = "/subedit/{subid}", method = RequestMethod.POST)
	public String subjectedit(RedirectAttributes redirectAttributes, Subject subject, @PathVariable int subid) {
		try {

			subject.setSubId(subid);
			subjectService.editSubject(subject);
			redirectAttributes.addFlashAttribute("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "redirect:/listsubject";

	}

	/**
	 * 删除科目信息
	 * 
	 * @param redirectAttributes
	 * @param subId
	 * @return
	 */
	@RequestMapping(value = "/deleteSubject/{subId}", method = RequestMethod.GET)
	public String delete(RedirectAttributes redirectAttributes, @PathVariable int subId) {
		try {
			subjectService.deleteSubject(subId);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/listsubject";
	}

	/**
	 * 获取所有学生
	 * @param subjectId
	 * @return
	 */
	@RequestMapping(value = "/getAllStudent", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> getAllStudent(Integer subjectId) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			resultMap.put("allUser", userDAO.findAllUsers());//所有学生
			resultMap.put("selectUser", userDAO.getAllUserBySubject(subjectId)); //选了该科目的学生
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
	
	/**
	 * 选择学生参考
	 * @param subjectStudents
	 * @param subjectId
	 * @return
	 */
	@RequestMapping(value = "/saveSubjectStudents", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saveSubjectStudents(@RequestParam(name= "subjectStudents[]")String[] subjectStudents,Integer subjectId){
		Map<String,Object> modalMap = new HashMap<String,Object>();
		
		List<String> list = new ArrayList<String>();
		for(String student: subjectStudents){
			list.add(student);
		}
		
		try{
			subjectService.saveSubjectStudent(list, subjectId);
		}catch (Exception e) {
			e.printStackTrace();
			modalMap.put("success", false);
		}
		modalMap.put("success", true);
		return modalMap;
	}
	
	/**
	 * 更新状态
	 * @param SubId
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/updateSubjectStatus", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateStatus(Integer SubId, Integer status)
	{
		Map<String,Object> modalMap = new HashMap<String,Object>();
		if(SubId>0 && status>=0){
			try {
				subjectService.updateStatus(SubId, status);
				modalMap.put("success", true);
			} catch (Exception e) {
				e.printStackTrace();
				modalMap.put("success", false);
				modalMap.put("errMsg", e.getMessage());
			}
		}else{
			modalMap.put("success", false);
			modalMap.put("errMsg", "SubId或status为空,非法更改状态");
		}
		return modalMap;
	}
}
